package mi3;

import org.activiti.engine.ProcessEngine;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.event.EventLogEntry;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class createTable {
//    public static void main(String[] args) {
//
//        //使用classpath下的activiti.cfg.xml中的配置创建processEngine
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        System.out.println(processEngine);
//    }

    // call 子任务
    @Test
    public void create() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        Deployment deploy = processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("ziliucheng.bpmn")
                .addClasspathResource("ziliucheng.png")
                .addClasspathResource("MyProcess.bpmn")
                .addClasspathResource("MyProcess.png")
                .deploy();
        String id = deploy.getId();
        System.out.println(id);
    }
    //  DEPLOYMENT_ID_ =  177501
    //ziliucheng   57501  62501


    @Test
    public void start(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HashMap<String, Object> map = new HashMap<>();
//        map.put("user1","老李");
        map.put("user2","老白");
        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("myProcess",map)
                ;
        System.out.println("实例id："+processInstance.getId());
        //实例id： 180001
    }

    @Test
    public void cheak(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .processInstanceId("40001")
                .list();
        for (Task task : list) {
            System.out.println(task.getId()+"----"+task.getName());
            //  37506  40006
        }
        System.out.println("");
    }
    @Test
    public void com(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        HashMap<String, Object> map = new HashMap<>();
        map.put("call","ziliucheng");
//        map.put("user1","老李");
        map.put("user2","老白");
//        RuntimeService runtimeService = processEngine.getRuntimeService();
//        runtimeService.setVariables("40001",map);
        processEngine.getRuntimeService().setVariables("180001",map);

        processEngine.getTaskService().complete("180005");

        //180005
        System.out.println();
        System.out.println("---"+"完成任务");
    }


    @Test
    public void cheakOut(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .processInstanceId("77504")
                .list();
        for (Task task : list) {
            System.out.println(task.getId()+"----"+task.getName());
            //  37506  40006
        }
        System.out.println("");
    }

    /*多实例任务*/

//    @Test
//    public void create() {
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        Deployment deploy = processEngine.getRepositoryService()
//                .createDeployment()
//                .addClasspathResource("process/test.bpmn")
//                .addClasspathResource("process/test.png")
////                .addClasspathResource("MyProcess.bpmn")
////                .addClasspathResource("MyProcess.png")
//                .deploy();
//        String id = deploy.getId();
//        System.out.println(id);
//    }
//    // act_re_procdef  DEPLOYMENT_ID_ =  95001  112501  125001
//
//
//    @Test
//    public void start(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        HashMap<String, Object> map = new HashMap<>();
////        map.put("user1","老李");
////        map.put("user2","老白");
////        map.put("user3","老黑");
//        List<String> assigneeList = new ArrayList<String>();
//        assigneeList.add("老李");
//        assigneeList.add("老白");
//        assigneeList.add("老黑");
//        assigneeList.add("老绿");
//        map.put("assigneeList",assigneeList);
////                                                                        .setVariables("assigneeList",map)
//        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("myProcess",map);
//        System.out.println("实例id："+processInstance.getId());
//        //实例id： 97501    117501   127501
//    }
//
//    @Test
//    public void cheak(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        List<Task> list = processEngine.getTaskService()
//                .createTaskQuery()
//                .processInstanceId("127501")
//                .list();
//        for (Task task : list) {
//            System.out.println(task.getId()+"----"+task.getName());
//            //  100012
//            //117517----多实例任务 //117522----多实例任务//117527----多实例任务//117532----多实例任务
//        }
//        System.out.println("");
//    }
//    @Test
//    public void com(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        TaskService taskService = processEngine.getTaskService();
//        //                                                                   .taskCandidateUser("老白")  //组任务分配给个人完成  ？ 为什么同一个组员可以多次完成 不是说同一组的完成了就会被移除吗 表中数据在哪里看
//        Task task = taskService.createTaskQuery().processInstanceId("127501").taskAssignee("老黑").singleResult();  //多实例列表  taskassignee
//
//        processEngine.getTaskService().complete(task.getId());
//
//
//        System.out.println();
//        System.out.println("任务id为"+task.getId()+"的任务完成");
//    }
//
//
//    @Test
//    public void cheakOut(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        List<Task> list = processEngine.getTaskService()
//                .createTaskQuery()
//                .processInstanceId("77504")
//                .list();
//        for (Task task : list) {
//            System.out.println(task.getId()+"----"+task.getName());
//            //  37506  40006
//        }
//        System.out.println("");
//    }

/*网关*/

//    @Test
//    public void create() {
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        Deployment deploy = processEngine.getRepositoryService()
//                .createDeployment()
//                .addClasspathResource("process/xxx/test1.bpmn")
//                .addClasspathResource("process/xxx/test1.png")
////                .addClasspathResource("MyProcess.bpmn")
////                .addClasspathResource("MyProcess.png")
//                .deploy();
//        String id = deploy.getId();
//        System.out.println(id);
//    }
//    // act_re_procdef  DEPLOYMENT_ID_ =  135001
//
//
//    @Test
//    public void start(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        HashMap<String, Object> map = new HashMap<>();
//
//        map.put("age",20);
////                                                                        .setVariables("assigneeList",map)
//        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("myProcess",map);
//        System.out.println("实例id："+processInstance.getId());
//        //实例id：   137501  145001
//    }
//
//    @Test
//    public void cheak(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        List<Task> list = processEngine.getTaskService()
//                .createTaskQuery()
//                .processInstanceId("145001")
//                .list();
//        for (Task task : list) {
//            System.out.println(task.getId()+"----"+task.getName());
//            //  137506  145006 老年路线
//        }
//        System.out.println("");
//    }
//    @Test
//    public void com(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        TaskService taskService = processEngine.getTaskService();
//
//        Task task = taskService.createTaskQuery().processInstanceId("145001").singleResult();
//
//        processEngine.getTaskService().complete(task.getId());
//
//
//        System.out.println();
//        System.out.println("任务id为"+task.getId()+"的任务完成");
//    }
//
//
//    @Test
//    public void cheakOut(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        TaskService taskService = processEngine.getTaskService();
//
//
//        List<Task> list = processEngine.getTaskService()
//                .createTaskQuery()
//                .executionId("145001")
////                .processInstanceId("145001")    //查不到任务？？？
//                .list();
//        for (Task task : list) {
//            System.out.println(task.getId()+"----"+task.getName());
//            //  37506  40006
//        }
//        System.out.println("");
//    }


//    /*监听器*/
//    @Test
//    public void create() {
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        Deployment deploy = processEngine.getRepositoryService()
//                .createDeployment()
//                .addClasspathResource("process/xxx/test1.bpmn")
//                .addClasspathResource("process/xxx/test1.png")
////                .addClasspathResource("MyProcess.bpmn")
////                .addClasspathResource("MyProcess.png")
//                .deploy();
//        String id = deploy.getId();
//        System.out.println(id);
//
//
//    }
//    // act_re_procdef  DEPLOYMENT_ID_ =  135001
//
//
//    @Test
//    public void start(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        HashMap<String, Object> map = new HashMap<>();
//
//        map.put("age",20);
////                                                                        .setVariables("assigneeList",map)
//        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("myProcess",map);
//        System.out.println("实例id："+processInstance.getId());
//        //实例id：   137501  145001
//    }
//
//    @Test
//    public void cheak(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        List<Task> list = processEngine.getTaskService()
//                .createTaskQuery()
//                .processInstanceId("145001")
//                .list();
//        for (Task task : list) {
//            System.out.println(task.getId()+"----"+task.getName());
//            //  137506  145006 老年路线
//        }
//        System.out.println("");
//    }
//    @Test
//    public void com(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        TaskService taskService = processEngine.getTaskService();
//
//        Task task = taskService.createTaskQuery().processInstanceId("145001").singleResult();
//
//        processEngine.getTaskService().complete(task.getId());
//
//
//        System.out.println();
//        System.out.println("任务id为"+task.getId()+"的任务完成");
//    }
//
//
//    @Test
//    public void cheakOut(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//
//        TaskService taskService = processEngine.getTaskService();
//
//
//        List<Task> list = processEngine.getTaskService()
//                .createTaskQuery()
//                .executionId("145001")
////                .processInstanceId("145001")    //查不到任务？？？
//                .list();
//        for (Task task : list) {
//            System.out.println(task.getId()+"----"+task.getName());
//            //  37506  40006
//        }
//        System.out.println("");
//    }




}
